package com.softer.chengxin.modify.service.impl;

import com.softer.chengxin.common.config.CourseException;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.common.util.ModifyUtil;
import com.softer.chengxin.corp.domin.Corp;
import com.softer.chengxin.modify.dao.BaseModifyDetailDao;
import com.softer.chengxin.modify.dao.CompanyModifyDao;
import com.softer.chengxin.modify.entity.BaseModifyDetail;
import com.softer.chengxin.modify.entity.CompanyModify;
import com.softer.chengxin.modify.service.ModifyService;
import com.softer.chengxin.project.domin.Project;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/9/11
 */
@Service
public class ModifyServiceImpl implements ModifyService {
    @Autowired
    private ModifyUtil<Corp> modifyUtil;
    @Autowired
    private ModifyUtil<Project> projectModifyUtil;
    @Autowired
    private CompanyModifyDao companyModifyDao;
    @Autowired
    private BaseModifyDetailDao baseModifyDetailDao;

    /**
     * 获取企业变更实体
     *
     * @param id 企业ID
     * @return 企业变更实体
     */
    @Override
    public Corp getCorpModify(int id) {
        return modifyUtil.getCompleteEntity(id, new Corp(), Common.ONE);
    }

    /**
     * 获取項目变更实体
     *
     * @param id 項目ID
     * @return 項目变更实体
     */
    @Override
    public Project getProjectModify(int id) {
        return projectModifyUtil.getCompleteEntity(id, new Project(), Common.ZERO);
    }

    /**
     * //1,插入变更表
     *
     * @param companyModify 变更实体
     * @param id            业务Id
     * @return 变更实体
     */
    public CompanyModify insertModify(CompanyModify companyModify, int id, String type) {
        if (companyModify == null) {
            companyModify = new CompanyModify();
            companyModify.setType(type);
            companyModify.setBusinessId(id);
            companyModify.setState(Common.ZERO);
            int count = companyModifyDao.insert(companyModify);
            if (count == 0) {
                throw new CourseException(ResultEnum.FAIL.getCode(), "新增变更失败");
            }
        }
        return companyModify;
    }

    /**
     * 处理变更详情
     *
     * @param difference 变更详情列表
     * @param modifyId   变更ID
     */
    public void dealModifyDetail(List<BaseModifyDetail> difference, int modifyId) {
        for (BaseModifyDetail b : difference) {
            String param = baseModifyDetailDao.getParam(modifyId, b.getParam());
            if (StringUtils.isNotBlank(param)) {
                continue;
            }
            b.setModifyId(modifyId);
            int insert = baseModifyDetailDao.insert(b);
            if (insert == 0) {
                throw new CourseException(ResultEnum.FAIL.getCode(), "新增变更详情失败");
            }
        }
    }

    /**
     * 查询是否存在变更记录，存在，则修改变更状态
     *
     * @param id 业务ID
     */
    public void updateState(int id) {
        //查询是否存在变更记录
        CompanyModify info = companyModifyDao.getInfo(id);
        if (info != null) {
            //修改变更状态
            int updateNum = companyModifyDao.updateState(id);
            if (updateNum == 0) {
                throw new CourseException(ResultEnum.FAIL.getCode(), "修改变更状态失败");
            }
        }
    }
}
